
import Qs from 'qs';
import { Portal, Toast } from '@ant-design/react-native';
let apiUrl = "http://weike.qh8866.cn:8082";

/**
 * 通用请求
 * 
 * @param {*} type 
 * @param {*} url 
 * @param {*} data 
 * @param {*} successCallback 
 * @param {*} errorCallback 
 * @param {*} completeCallback 
 */
function request(type, url, data, successCallback, errorCallback, completeCallback) {
    switch (type) {
        case "POST":
            fetch(url.substr(0, 4).toLowerCase() === "http" ? url : apiUrl + url, {
                method: type,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: data
            }).then(_ => _.json()).then((res) => {
                if (res) {
                    if (res.res == 0) {
                        Toast.fail(res.msg)
                        errorCallback(res);
                    } else {
                        successCallback(res);
                    }
                } else {
                    Toast.fail("服务器开小差了")
                    errorCallback(res);
                }
            }).catch(err => (errorCallback(err))).finally(() => completeCallback());
            break;
        case "GET":
            fetch(url.substr(0, 4).toLowerCase() === "http" ? url : apiUrl + url).then(_ => _.json()).then((res) => {
                if (res) {
                    if (res.res == 0) {
                        Toast.fail(res.msg)
                        errorCallback(res);
                    } else {
                        successCallback(res);
                    }
                } else {
                    Toast.fail("服务器开小差了")
                    errorCallback(res);
                }
            }).catch(err => (errorCallback(err))).finally(() => completeCallback());
            break;
    }
}


/**
 * 封装get方法
 * 
 * @param {*} url
 * @param {*} params
 * @returns {Promise}
 */
function get(url, params, show = true, desc = "加载中...") {
    let key = 0;
    if (show) { key = Toast.loading(desc) }
    params['_appId'] = 'weike';
    return new Promise((resolve, reject) => {
        request('GET', url, Qs.stringify(params), (res) => {
            if (show) { Portal.remove(key) }
            resolve(res)
        }, (res) => {
            if (show) { Portal.remove(key) }
            reject(res)
        }, () => {
            if (show) { Portal.remove(key) }
        })
    })
}

/**
 * 封装post请求
 * 
 * @param {*} url 
 * @param {*} data 
 * @returns {Promise}
 */
function post(url, params, show = true, desc = "加载中...") {
    let key = 0;
    if (show) { key = Toast.loading(desc) }
    params['_appId'] = 'weike';
    return new Promise((resolve, reject) => {
        request('POST', url, Qs.stringify(params), (res) => {
            if (show) { Portal.remove(key) }
            resolve(res)
        }, (res) => {
            if (show) { Portal.remove(key) }
            reject(res)
        }, () => {
            if (show) { Portal.remove(key) }
        })
    })
}

const API = { get, post }
export { API };